package com.flower.controller;

import com.flower.config.RedisSupport;
import io.netty.util.internal.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * 延迟队列处理器
 *
 * @author shanks on 2019/5/15
 */
@RestController
@Slf4j
public class DelayQueueController {

    @Autowired
    private RedisSupport redisSupport;

    /**
     * 放入延迟消息
     *
     * @param topic 主题
     * @param delay 延迟时间
     * @param body  业务消息
     * @return
     */
    @RequestMapping(value = "delay/sendMessageDelayed")
    @ResponseBody
    public Map<String, Object> sendMessageDelayed(@RequestParam String topic,
                                          @RequestParam int delay,
                                          @RequestParam String body) {
        log.info("主题：{}，延时时间：{}(秒)，业务消息：{}", topic, delay, body);
        long timeOut = System.currentTimeMillis() / 1000 + delay;
        boolean isSucc = redisSupport.zadd(topic, body, timeOut);
        log.info("消息添加成功：{}", isSucc);
        Map<String, Object> result = new HashMap<>(16);
        if (isSucc){
            result.put("code", "000000");
            result.put("message", "交易成功");
        }else{
            result.put("code", "100000");
            result.put("message", "消息重复投递");
        }
        return result;
    }

}
